package com.ycsj.goldmedalnewconcept.common.util;

import android.text.TextUtils;
import android.util.Log;

import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.TimeResponse;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 公共的做题计时请求接口
 * Created by Administrator on 2019/4/8
 */

public class TestTimeCountUtil {

    /**
     * 开始做题计时请求接口
     * exam_type  1：词汇检测  2：预习检测  3：词汇预习  4：语法检测  5：微课堂  6：题库练习  9：跟读
     */
    public static void startTime(Object object, String type, String gradeid, String lessonid, final onSuccessListener listener) {
        Log.i("test","acc="+AccountUtils.getAccount(BaseApplication.context));
        Log.i("test","role="+AccountUtils.getRole(BaseApplication.context));
        if(!TextUtils.isEmpty(AccountUtils.getAccount(BaseApplication.context))&&
                !TextUtils.isEmpty(AccountUtils.getRole(BaseApplication.context))){


        Map<String, String> map = new HashMap<>();
        map.put("type", "6");
        map.put("acc", AccountUtils.getAccount(BaseApplication.context));
        map.put("role", AccountUtils.getRole(BaseApplication.context));
        map.put("exam_type", type);
        map.put("gradeid", gradeid);
        map.put("lessonid", lessonid);
        map.put("platform", "2");


        Log.i("test","acc="+AccountUtils.getAccount(BaseApplication.context)+
                "role="+AccountUtils.getRole(BaseApplication.context));
        HttpUtils.post(HttpAddress.TESTSTART, map, object, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }

            @Override
            public void onResponse(String response, int id) {
                TimeResponse info = null;
                try {
                    info = GsonUtil.jsonToBean(response, TimeResponse.class);
                    Log.i("test", "timejson："+response);
                } catch (Exception e) {
                    Log.i("test", "数据解析异常");
                }
                if (info != null) {
                    listener.onSuccess(info.rid);
                }
            }
        });
        }
    }

    /**
     * 停止做题计时请求接口
     */
    public static void stopTime(Object object, String rid) {

        if (!TextUtils.isEmpty(rid)) {

            Map<String, String> map = new HashMap<>();
            map.put("type", "7");
            map.put("acc", AccountUtils.getAccount(BaseApplication.context));
            map.put("role", AccountUtils.getRole(BaseApplication.context));
            map.put("rid", rid);
            map.put("platform", "2");

            HttpUtils.post(HttpAddress.TESTSTOP, map, object, new HttpUtils.CallBack() {
                @Override
                public void onError(Call call, Exception e, int id) {
                }

                @Override
                public void onResponse(String response, int id) {
                }
            });
        }
    }

    /**
     * 回调接口
     * 成功获取rid将数据回调出去
     */
    public interface onSuccessListener {
        void onSuccess(String infoRid);

    }
}
